﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.admin.product
{
    public partial class prosory : System.Web.UI.Page
    {
    

        // Methods
        private void Bind()
        {
            string sql = "select * from DB_Productclass where visible=1 and languages=" + info.languages("") + " and parentid=268 order by classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(row["classname"].ToString(), row["classid"].ToString()));
                }
            }
            if ((base.Request.QueryString["pid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "yes1")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisshow=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "no1")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisshow=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "yes2")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew1=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "no2")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew1=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "yes3")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew2=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "no3")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew2=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "yes4")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew3=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "no4")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew3=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "yes5")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew4=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "no5")
                {
                    SQLAccess.DoSql("update DB_Shoppro set proisnew4=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("update DB_Shoppro set visible=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                    base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
                if (base.Request.QueryString["action"].ToString() == "copy")
                {
                    DataTable table2 = SQLAccess.GreatDs("select * from DB_Shoppro where proid=" + base.Request.QueryString["pid"] + "").Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        DataTable table3 = SQLAccess.GreatDs("select * from DB_Memberprice where productid=" + table2.Rows[0]["proid"].ToString() + "").Tables[0];
                        if (table3.Rows.Count > 0)
                        {
                            foreach (DataRow row2 in table3.Rows)
                            {
                                SQLAccess.DoSql("insert into DB_Memberprice (rankid,productid,price) values (" + row2["rankid"].ToString() + "," + re.maxid("", "DB_Shoppro", "proid") + "," + row2["price"].ToString() + ")");
                            }
                        }
                        SQLAccess.DoSql(string.Concat(new object[] { 
                        "insert into DB_Shoppro (proid,proname,procode,typeid,propinpai,proprice1,proprice2,proprice3,proscore1,proscore2,proprice4,iscuxao,prodate1,prodate2,propic1,propic2,procontent,proweight1,proweight2,proku1,proku2,proisnew1,proisnew2,proisnew3,proisshow,proisp,prokey,promsg,promark,prosize,procolor,visible,pronameen,procontenten,updatetime,languages,clicks,proorder,protype) values (", re.maxid("", "DB_Shoppro", "proid"), ",'", re.replace(table2.Rows[0]["proname"].ToString()), "','", re.replace(table2.Rows[0]["procode"].ToString()), "',", table2.Rows[0]["typeid"].ToString(), ",", table2.Rows[0]["propinpai"].ToString(), ",", table2.Rows[0]["proprice1"].ToString(), ",", table2.Rows[0]["proprice2"].ToString(), ",", table2.Rows[0]["proprice3"].ToString(), 
                        ",", table2.Rows[0]["proscore1"].ToString(), ",", table2.Rows[0]["proscore2"].ToString(), ",", table2.Rows[0]["proprice4"].ToString(), ",", (bool) table2.Rows[0]["iscuxao"], ",'", re.replace(table2.Rows[0]["prodate1"].ToString()), "','", re.replace(table2.Rows[0]["prodate2"].ToString()), "','", re.replace(table2.Rows[0]["propic1"].ToString()), "','", re.replace(table2.Rows[0]["propic2"].ToString()), 
                        "','", re.replace(table2.Rows[0]["procontent"].ToString()), "','", re.replace(table2.Rows[0]["proweight1"].ToString()), "','", re.replace(table2.Rows[0]["proweight2"].ToString()), "',", table2.Rows[0]["proku1"].ToString(), ",", table2.Rows[0]["proku2"].ToString(), ",", (bool) table2.Rows[0]["proisnew1"], ",", (bool) table2.Rows[0]["proisnew2"], ",", (bool) table2.Rows[0]["proisnew3"], 
                        ",", (bool) table2.Rows[0]["proisshow"], ",", (bool) table2.Rows[0]["proisp"], ",'", re.replace(table2.Rows[0]["prokey"].ToString()), "','", re.replace(table2.Rows[0]["promsg"].ToString()), "','", re.replace(table2.Rows[0]["promark"].ToString()), "','", re.replace(table2.Rows[0]["prosize"].ToString()), "','", re.replace(table2.Rows[0]["procolor"].ToString()), "',", (bool) table2.Rows[0]["visible"], 
                        ",'", re.replace(table2.Rows[0]["pronameen"].ToString()), "','", re.replace(table2.Rows[0]["procontenten"].ToString()), "','", re.replace(table2.Rows[0]["updatetime"].ToString()), "',", table2.Rows[0]["languages"].ToString(), ",", table2.Rows[0]["clicks"].ToString(), ",", table2.Rows[0]["proorder"].ToString(), ",1)"
                     }));
                    }
                    base.Response.Write("<script language='javascript'>alert('复制成功！');window.location.href='prosory.aspx';</script>");
                    base.Response.End();
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind("");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                HyperLink link = (HyperLink)e.Row.FindControl("HyperLink1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        link.NavigateUrl = fun.geturl1("") + "products_details.aspx?categoryid=" + re.webinfo2("", e.Row.Cells[8].Text, info.languages("")) + "&typeid=" + e.Row.Cells[8].Text + "&pid=" + e.Row.Cells[7].Text;
                        e.Row.Cells[1].Width = 150;
                        if (e.Row.Cells[5].Text == "True")
                        {
                            e.Row.Cells[5].Text = "<a href=prosory.aspx?action=yes1&amp;pid=" + e.Row.Cells[8].Text + "><img src='../images/yes.gif' border='0' /></a>";
                        }
                        else
                        {
                            e.Row.Cells[5].Text = "<a href=prosory.aspx?action=no1&amp;pid=" + e.Row.Cells[8].Text + "><img src='../images/no.gif' border='0' /></a>";
                        }
                        if (e.Row.Cells[6].Text == "True")
                        {
                            e.Row.Cells[6].Text = "<a href=prosory.aspx?action=yes2&amp;pid=" + e.Row.Cells[8].Text + "><img src='../images/yes.gif' border='0' /></a>";
                        }
                        else
                        {
                            e.Row.Cells[6].Text = "<a href=prosory.aspx?action=no2&amp;pid=" + e.Row.Cells[8].Text + "><img src='../images/no.gif' border='0' /></a>";
                        }
                        e.Row.Cells[4].Text = info.info1("", "shophuobi") + e.Row.Cells[4].Text;
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                    e.Row.Cells[8].Visible = false;
                    e.Row.Cells[9].Visible = false;
                }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sqlsort = "";
            sqlsort = "select * from DB_Shoppro where visible=1 and langauges=" + info.languages("") + " ";
            if (this.ishot.Value.ToString() == "1")
            {
                sqlsort = sqlsort + " and proisnew1=1";
            }
            if (this.ishot.Value.ToString() == "2")
            {
                sqlsort = sqlsort + " and proisnew2=1";
            }
            if (this.ishot.Value.ToString() == "3")
            {
                sqlsort = sqlsort + " and proisnew3=1";
            }
            if (this.ishot.Value.ToString() == "4")
            {
                sqlsort = sqlsort + " and proisnew4=1";
            }
            if (this.typeid.SelectedValue.ToString() != "0")
            {
                sqlsort = sqlsort + " and typeid in(" + re.returntype("0", this.typeid.SelectedValue.ToString(), info.languages("")) + ")";
            }
            if (re.replace(this.keyname.Value.ToString()) != "")
            {
                sqlsort = sqlsort + " and (proname like '%" + re.replace(this.keyname.Value.ToString()) + "%' or procode like '%" + re.replace(this.keyname.Value.ToString()) + "%')";
            }
            this.GridViewBind(sqlsort);
        }

        private void GridViewBind(string Sqlsort)
        {
            string sql = "select * from DB_Shoppro where visible=1 and languages=" + info.languages("") + " and protype=1 ";
            if (this.ishot.Value.ToString() == "1")
            {
                sql = sql + " and proisnew1=1";
            }
            if (this.ishot.Value.ToString() == "2")
            {
                sql = sql + " and proisnew2=1";
            }
            if (this.ishot.Value.ToString() == "3")
            {
                sql = sql + " and proisnew3=1";
            }
            if (this.ishot.Value.ToString() == "4")
            {
                sql = sql + " and proisnew4=1";
            }
            if (this.typeid.SelectedValue.ToString() != "268")
            {
                sql = sql + " and typeid in(" + re.returntype(this.typeid.SelectedValue.ToString(), this.typeid.SelectedValue.ToString(), info.languages("")) + ")";
            }
            if (re.replace(this.keyname.Value.ToString()) != "")
            {
                sql = sql + " and (proname like '%" + re.replace(this.keyname.Value.ToString()) + "%' or procode like '%" + re.replace(this.keyname.Value.ToString()) + "%')";
            }
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                TextBox box2 = (TextBox)this.GridView1.Rows[i].FindControl("TextBox1");
                if (box.Checked)
                {
                    if (this.selectid.Value.ToString() == "0")
                    {
                        SQLAccess.DoSql(string.Concat(new object[] { "update DB_Shoppro set proorder=", box2.Text.Trim() == "" ? "0" : re.replace(box2.Text), " where proid=", this.GridView1.DataKeys[i].Value, "" }));
                    }
                    if (this.selectid.Value.ToString() == "1")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set visible=0 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "2")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisshow=1 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "3")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisshow=0 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "4")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew1=1 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "5")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew1=0 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "6")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew2=1 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "7")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew2=0 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "8")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew3=1 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "10")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew4=1 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "11")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew4=0 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                    if (this.selectid.Value.ToString() == "9")
                    {
                        SQLAccess.DoSql("update DB_Shoppro set proisnew3=0 where proid=" + this.GridView1.DataKeys[i].Value + "");
                    }
                }
            }
            base.Response.Write("<script language=javascript>alert('更新成功!');window.location.href='prosory.aspx';</script>");
            base.Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton2.Attributes["onclick"] = "return CheckSelect(true,'update');";
            if (!base.IsPostBack)
            {
                this.GridViewBind("");
                this.Bind();
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind("");
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}